import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter_ahj_app/app/APIConfiguration.dart';

class ApiService {
  ApiService(this.dio);

  Dio dio;

  //get请求结构
  Future _get(String url, {Map<String, dynamic> params}) async {
    var response = await dio.get(url, queryParameters: params);
    print(response.data);
    if (url == "auth/oauth/token") {
      // 登录接口获取cookie
      if (response.statusCode == 200) {
        try {
          if (response.data != null && response.data['code'] == 0) {
            //登录成功获取cookie
            Headers headers = response.headers;
            if (headers != null &&
                headers.map != null &&
                headers.map["sid"] != null &&
                headers.map["sid"].length > 0) {
              String sid = headers.map["sid"][0];
              if (sid != null && sid != "") {
              }
              print(sid);
            }
          }
        } catch (e) {
          //登录失败不操作
        }
      }
    }
    return response.data;
  }

  //post
  Future _post(String url,var params) async {
    var response = await dio.post(url, data: json.encode(params));
    return response.data;
  } //post

  //post list
  Future _postList(String url, List params) async {
    var response = await dio.post(url, data: json.encode(params));
    return response.data;
  } //post

  Future _postfrom(String url, Map<String, dynamic> params) async {
    var response = await dio.post(url, queryParameters: params);
    return response.data;
  }

  //put

  Future _put(String url, Map<String, dynamic> params) async {
    var response = await dio.put(url, data: json.encode(params));
    return response.data;
  }

  Future _putMap(String url, Map<String, dynamic> params) async {
    var response = await dio.put(url, data: params);
    return response.data;
  }

  Future _delete(String url, Map<String, dynamic> params) async {
    var response = await dio.delete(url, data: json.encode(params));
    return response.data;
  }

  Future _deletedata(String url, Map<String, dynamic> params) async {
    var response = await dio.delete(url, data: json.encode(params));
    return response.data;
  }

  //post
  Future _postVerision(String url, Map<String, dynamic> params) async {
    var response = await dio.post(url, queryParameters: params);
    if (url == check_code_login) {
      // 登录接口获取cookie
      if (response.statusCode == 200) {
        try {
          if (response.data != null && response.data['code'] == 0) {
            //登录成功获取cookie
            Headers headers = response.headers;
            if (headers != null && headers.map != null) {
              if (headers.map["sid"] != null && headers.map["sid"].length > 0) {
                String sid = headers.map["sid"][0];
                if (sid != null && sid != "") {
                  // response.data = userBeanEntityToJson(response.data);
                }
              }
              if (headers.map["pw"] != null && headers.map["pw"].length > 0) {
                String pw = headers.map["pw"][0];
                if (pw != null && pw != "") {
                  // response.data = userBeanEntityToJson(response.data);
                }
              }
            }
          }
        } catch (e) {
          //登录失败不操作
        }
      }
    }
    return response.data;
  }

  Stream postVersion(String url, Map<String, dynamic> params) =>
      Stream.fromFuture(_postVerision(url, params)).asBroadcastStream();

  Stream post(String url, var params) =>
      Stream.fromFuture(_post(url, params)).asBroadcastStream();
  Stream postList(String url, List params) =>
      Stream.fromFuture(_postList(url, params)).asBroadcastStream();

  Stream postfom(String url, Map<String, dynamic> params) =>
      Stream.fromFuture(_postfrom(url, params)).asBroadcastStream();

  Stream put(String url, Map<String, dynamic> params) =>
      Stream.fromFuture(_put(url, params)).asBroadcastStream();

  Stream putMap(String url, {Map<String, dynamic> params}) =>
      Stream.fromFuture(_putMap(url, params)).asBroadcastStream();

  Stream get(String url, {Map<String, dynamic> params}) =>
      Stream.fromFuture( _get(url, params: params)).asBroadcastStream();

  Stream delete(String url, {Map<String, dynamic> params}) =>
      Stream.fromFuture(_delete(url, params)).asBroadcastStream();

  Stream deletedata(String url, {Map<String, dynamic> params}) =>
      Stream.fromFuture(_deletedata(url, params)).asBroadcastStream();
}
